{
  "name": "Deal Exception & Discount Approval Agent",
  "nodes": [
    {
      "parameters": {
        "updates": ["message"]
      },
      "id": "tg-trigger-15",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "name": "user_message",
              "value": "={{ $json.message.text }}",
              "type": "string"
            },
            {
              "name": "user_name",
              "value": "={{ $json.message.from.first_name + ' ' + ($json.message.from.last_name || '') }}",
              "type": "string"
            },
            {
              "name": "chat_id",
              "value": "={{ $json.message.chat.id }}",
              "type": "string"
            }
          ]
        }
      },
      "id": "set-context-15",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 0]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=User Name: {{ $json.user_name }}\nUser Message: {{ $json.user_message }}\nChat ID: {{ $json.chat_id }}",
        "options": {
          "systemMessage": "=You are a Deal Exception and Discount Approval Assistant.\n\nYour responsibilities:\n1. Identify whether the user wants to REGISTER a new deal discount exception or CHECK the status of an existing exception.\n\n2. For new exception requests, collect:\n   - Deal ID\n   - Requested Discount Percentage\n   - Reason for discount exception\n\n3. Validate the deal using the Deal Exception Master Tool and extract:\n   - Account_Name\n   - Account_Status\n   - Account_Owner_Email (used for email communication)\n\n4. If the deal is valid and active:\n   - Generate a unique exception reference ID in the format:\n     DEX-YYYYMMDD-[last 4 digits of deal]-HHmmss\n   - Register the exception using the Deal Exception Register Tool\n   - Send an acknowledgement email using the Gmail Tool to Account_Owner_Email\n   - Clearly explain approval workflow and expected timelines\n   - Respond using the Telegram Response Tool\n\n5. If the deal is invalid or inactive:\n   - Politely explain the reason\n   - Do NOT register the exception\n   - Respond using the Telegram Response Tool\n\n6. For status checks:\n   - Ask for exception reference ID if missing\n   - Retrieve current status using the Deal Exception Status Tool\n   - Explain the current approval stage and next steps\n   - Respond using the Telegram Response Tool\n\nStrict rules:\n- Do NOT promise approval.\n- Do NOT provide pricing or commercial advice.\n- Account owner email must be retrieved from the master sheet only.\n- ALWAYS use the Telegram Response Tool."
        }
      },
      "id": "agent-15",
      "name": "Deal Exception Approval Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [500, 0]
    },
    {
      "parameters": {
        "model": { "mode": "id", "value": "gpt-4o" },
        "options": { "temperature": 0.2 }
      },
      "id": "llm-15",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [320, 220]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Workflow Configuration').item.json.chat_id }}"
      },
      "id": "memory-15",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [500, 220]
    },
    {
      "parameters": {
        "descriptionType": "manual"
      },
      "id": "deal-master-15",
      "name": "Deal Exception Master Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [640, 260]
    },
    {
      "parameters": {
        "operation": "append",
        "descriptionType": "manual"
      },
      "id": "deal-register-15",
      "name": "Deal Exception Register Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [800, 260]
    },
    {
      "parameters": {
        "descriptionType": "manual"
      },
      "id": "deal-status-15",
      "name": "Deal Exception Status Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [960, 320]
    },
    {
      "parameters": {
        "sendTo": "={{ $fromAI('account_owner_email') }}",
        "subject": "={{ $fromAI('email_subject') }}",
        "message": "={{ $fromAI('email_body') }}"
      },
      "id": "gmail-15",
      "name": "Deal Exception Notification Email",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [1120, 220]
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}"
      },
      "id": "tg-response-15",
      "name": "Send Telegram Response",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [960, 0]
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [[{ "node": "Workflow Configuration", "type": "main" }]]
    },
    "Workflow Configuration": {
      "main": [[{ "node": "Deal Exception Approval Agent", "type": "main" }]]
    },
    "Deal Exception Approval Agent": {
      "main": [[{ "node": "Send Telegram Response", "type": "main" }]]
    }
  },
  "active": false
}
